package com.qdlc.p2p.manage.action.additional;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.EnjoyUserService;
import com.qdlc.p2p.biz.util.excel.ExcelUtils;
import com.qdlc.p2p.biz.util.excel.JsGridReportBase;
import com.qdlc.p2p.biz.util.excel.TableData;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.model.EnjoyUserModel;

/**
 * Created by D on 2016/1/25.
 */
public class ManageEnjoyUserAction extends BaseAction<EnjoyUserModel> implements ModelDriven<EnjoyUserModel> {

    @Resource
    private EnjoyUserService enjoyUserService;

    private Map<String, Object> data;

    @Action("/modules/additional/enjoyUser/manage")
    public String manage(){
        return "manage";
    }

    /**
     * 获取列表信息
     * @throws IOException
     */
    @Action("/modules/additional/enjoyUser/list")
    public void list() throws Exception{
        data = new HashMap<String, Object>();
        PageDataList<EnjoyUserModel> pagaDataList = enjoyUserService.findByModel(model);
        int total = pagaDataList.getPage().getTotal();
        data.put("total", total);
        data.put("rows", pagaDataList.getList());
        printJson(getStringOfJpaObj(data));
    }

    /**
     * 导出EXCEL列表
     */
    @Action("/modules/additional/enjoyUser/exportExcelList")
    public void exportExcelList() throws Exception{
        model.setPage(1);
        model.setRows(5000);
        PageDataList<EnjoyUserModel> pageDataList = enjoyUserService.findByModel(model);

        String title = "趣享用户商品Excel表";

        String[] hearders = new String[] { "编号", "用户名", "标名称", "商品名称", "数量", "收货地址" };// 表头数组
        String[] fields = new String[] { "id", "userName", "borrowName","goodsName", "amount", "receiveInfo" };// 对象属性数组
        TableData td = ExcelUtils.createTableData(pageDataList.getList(), ExcelUtils.createTableHeader(hearders), fields);
        JsGridReportBase report = new JsGridReportBase(request, response);
        report.exportToExcel(title, getOperator().getUserName(), td);
    }


}
